/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

// 先算出两个链表的差值长度，再循环每个进行比对，有相同就返回。
function getLength(head) {
    if(head===null) return 0;
    let p = head;
    let length = 0;
    while(p!=null){
        length++
        p = p.next;
    } 
    return length;
}
/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
    let n1 = getLength(headA);
    let n2 = getLength(headB);
    let count;
    let curA = headA;
    let curB = headB;
    if(n1>n2) {
        count = n1 - n2;
        while(count--) {
            curA = curA.next;
        }
    }else {
        count = n2 - n1;
        while(count--) {
            curB = curB.next;
        }
    }

    while(curA!=null&&curB!=null) {
        if(curA===curB) return curA;
        curA = curA.next;
        curB = curB.next;
    }
    return null;

};

